﻿using MES.Core.Filter;
using Microsoft.AspNetCore.Http;
using Microsoft.Extensions.Configuration;
using Microsoft.Extensions.DependencyInjection;
using Microsoft.Extensions.DependencyInjection.Extensions;
using Microsoft.Extensions.Options;
using SqlSugar;

namespace MES.Core
{
    public static class AppSetting
    {
        public static IServiceCollection _Services;

        public static T GetService<T>()
        {
            var provider = _Services.BuildServiceProvider();
            return provider.GetRequiredService<T>();
        }

        /// <summary>
        /// 添加服务
        /// </summary>
        /// <param name="interfaceType"></param>
        /// <param name="implementationType"></param>
        public static void AddService(Type interfaceType, Type implementationType, ServiceLifetime lifetime, Type interfaceDBType = null, Type implementationDBType = null)
        {
            var serviceCollection = new ServiceCollection();

            switch (lifetime)
            {
                case ServiceLifetime.Scoped:
                    serviceCollection.Add(new ServiceDescriptor(interfaceType, implementationType, ServiceLifetime.Scoped));
                    if (interfaceDBType != null && implementationDBType != null)
                    {
                        serviceCollection.Add(new ServiceDescriptor(interfaceDBType, implementationDBType, ServiceLifetime.Scoped));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IHttpContextAccessor), typeof(HttpContextAccessor), ServiceLifetime.Scoped));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IHttpHeaderAccessor), typeof(HttpHeaderAccessor), ServiceLifetime.Scoped));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IServiceCollection), typeof(ServiceCollection), ServiceLifetime.Scoped));
                    }
                    break;
                case ServiceLifetime.Singleton:
                    serviceCollection.Add(new ServiceDescriptor(interfaceType, implementationType, ServiceLifetime.Singleton));
                    if (interfaceDBType != null && implementationDBType != null)
                    {
                        serviceCollection.Add(new ServiceDescriptor(interfaceDBType, implementationDBType, ServiceLifetime.Singleton));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IHttpContextAccessor), typeof(HttpContextAccessor), ServiceLifetime.Singleton));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IHttpHeaderAccessor), typeof(HttpHeaderAccessor), ServiceLifetime.Singleton));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IServiceCollection), typeof(ServiceCollection), ServiceLifetime.Singleton));
                    }
                    break;
                case ServiceLifetime.Transient:
                    serviceCollection.Add(new ServiceDescriptor(interfaceType, implementationType, ServiceLifetime.Transient));
                    if (interfaceDBType != null && implementationDBType != null)
                    {
                        serviceCollection.Add(new ServiceDescriptor(interfaceDBType, implementationDBType, ServiceLifetime.Transient));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IHttpContextAccessor), typeof(HttpContextAccessor), ServiceLifetime.Transient));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IHttpHeaderAccessor), typeof(HttpHeaderAccessor), ServiceLifetime.Transient));
                        serviceCollection.Add(new ServiceDescriptor(typeof(IServiceCollection), typeof(ServiceCollection), ServiceLifetime.Transient));
                    }
                    break;
            }
            var serviceProvider = serviceCollection.BuildServiceProvider();
            var service = serviceProvider.GetService(interfaceType);
        }
        #region 静态 Configuration 用来获取未配置的节点

        public static IConfiguration Configuration { get; private set; }

        /// <summary>
        ///数据库连接配置
        /// </summary>
        public static ConnectionConfig connectionConfigs { get; set; }


        #endregion 静态 Configuration 用来获取未配置的节点

        public static void BuildConfiguration(this IServiceCollection services)
        {
            _Services = services;
            var provider = services.BuildServiceProvider();
            Configuration = provider.GetRequiredService<IConfiguration>();
            services.Configure<ConnectionConfig>(Configuration.GetSection("MainDB"));
            provider = services.BuildServiceProvider();
            connectionConfigs = provider.GetRequiredService<IOptions<ConnectionConfig>>().Value;

        }
    }
}
